package Strategy;

/**
 * <p>Title Hand</p>
 *
 * @author YiMing Liu
 * @date 2018/6/12 14:29
 * <p>Description:</p>
 */
public class Hand {
    public static final int HADNVALUE_GUU = 0; //石头
    public static final int HADNVALUE_CHO = 1; //剪刀
    public static final int HADNVALUE_PAA = 2; //布

    private int handValue; //手势的值

    public Hand(int handValue) {
        this.handValue = handValue;
    }

    public static final Hand[] hand = {  //猜拳中手的三个实例
            new Hand(HADNVALUE_GUU),
            new Hand(HADNVALUE_CHO),
            new Hand(HADNVALUE_PAA),
    };

    private static final String[] name = { //手势的名字
            "石头", "剪刀", "布"
    };

    public static Hand getHand(int handValue) { //根据手势的值返回手势的实例
        return hand[handValue];
    }

    private int fight(Hand h) { //计分函数 相同0分，获胜+1，失败-1
        if (this == h) {
            return 0;
        } else if ((this.handValue + 1) % 3 == h.handValue) {
            return 1;
        } else {
            return -1;
        }
    }

    public boolean isStrongerThen(Hand h) {  //判断this是否获胜h
        return fight(h) == 1;
    }

    public boolean isWeakerThen(Hand h) {  //判断this是否败于h
        return fight(h) == -1;
    }

    public String toString() {  //转换手势值所对应的字符串
        return name[handValue];
    }
}
